package cn.liangjintang.foldbrowerbundle;

import java.io.File;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.widget.EditText;

/**
 * 
 * 这是一个文件夹对话框管理器,你可以从这里获得一个文件夹对话框实例
 * 
 */
public class FoldBrowerDialog {
	private Context ct;
	private FoldDialogOKListener fdokl;

	public FoldBrowerDialog() {

	}

	/**
	 * 显示一个对话框
	 * 
	 * @param context
	 *            上下文
	 * @param dirs
	 *            目录
	 * @param foldDialogOKListener
	 *            对话框的确定按钮事件监听器
	 */
	public final void dirDialog(final Context context, File dirs,
			FoldDialogOKListener foldDialogOKListener) {
		ct = context;
		fdokl = foldDialogOKListener;

		final FoldViewBuilder foldViewBuilder = new FoldViewBuilder(context);
		foldViewBuilder.setDataAndShow(dirs);
		new AlertDialog.Builder(context)
				//.setTitle("选择文件夹")
				.setView(foldViewBuilder.foldView)
				.setPositiveButton("确定", new OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						if (fdokl != null)
							fdokl.onOK(foldViewBuilder);
					}
				})
				.setNeutralButton("新建文件夹",
						new OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								newDirdialog(foldViewBuilder.selectedFileDir);
							}
						})
				.setNegativeButton("取消", new OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						
					}
				})
				.show();
	}

	private final void newDirdialog(final File dir) {
		final EditText et = new EditText(ct);
		new AlertDialog.Builder(ct)
				.setTitle("文件夹名称")
				.setView(et)
				.setPositiveButton("确定", new OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						File file = new File(dir, et.getText().toString());
						file.mkdir();
						dirDialog(ct, dir, fdokl);
					}
				})
				.setNegativeButton("取消", new OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dirDialog(ct, dir, fdokl);
					}
				}).show();
	}
}
